package com.softer.chengxin.sysscore.controller;

import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.JsonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.sysscore.entity.SysScoreTypeDTO;
import com.softer.chengxin.sysscore.service.SysScoreTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/12
 */
@Slf4j
@RestController
@RequestMapping("/api/sysScoreType")
public class SysScoreTypeController {
    @Autowired
    private SysScoreTypeService sysScoreTypeService;
    /**
     * 获取评分列表
     * @param session 缓存信息
     * @return 企业信用得分
     */
    @RequestMapping(value = "/getList")
    public ManagerPageResult getList(Page page, SysScoreTypeDTO scoreTypeDTO, HttpSession session) {
        log.info("获取评分列表开始，评分类型："+scoreTypeDTO.getScoreType());
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        List<SysScoreTypeDTO> scoreTypeList = sysScoreTypeService.getScoreTypeList(page, scoreTypeDTO);
        int count = sysScoreTypeService.getScoreTypeCount(scoreTypeDTO);
        jr.setDs(scoreTypeList);
        mpr.setJson(jr);
        mpr.setCount(count);
        log.info("获取评分列表结束！count:"+count);
        return mpr;
    }

    /**
     * 增加评分类型
     * @param list    评分数据集合
     * @param session 缓存信息
     * @return 处理结果
     */
    @RequestMapping("/add")
    public CommonResult add(String list, String scoreType, HttpSession session) {
        //登陆名
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)){
            log.info("请重新登陆后操作！");
            return new CommonResult(ResultEnum.FAIL.getCode(),"请重新登陆后操作！");
        }
        CommonResult commonResult = sysScoreTypeService.add(list, scoreType, loginName);
        log.info("添加评分类型结束！data："+commonResult.getData());
        return commonResult;
    }
    /**
     * 增加评分类型
     * @param scoreTypeDTO    数据集合
     * @param session 缓存信息
     * @return 处理结果
     */
    @RequestMapping("/update")
    public CommonResult update(SysScoreTypeDTO scoreTypeDTO, HttpSession session) {
        log.info("修改评分类型开始！");
        //登陆名
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)){
            return new CommonResult(ResultEnum.FAIL.getCode(),"请重新登陆后操作");
        }
        CommonResult commonResult = sysScoreTypeService.update(scoreTypeDTO);
        log.info("修改评分类型结束！data:"+commonResult.getData());
        return commonResult;
    }
    /**
     * 删除
     * @param id
     * @param session 缓存信息
     * @return 处理结果
     */
    @RequestMapping("/delete")
    public CommonResult delete(String id, HttpSession session) {
        //登陆名
        String loginName = (String) session.getAttribute("loginName");
        log.info("删除评分类型开始！id："+id);
        if (StringUtils.isBlank(loginName)){
            log.info("请重新登陆后操作！");
            return new CommonResult(ResultEnum.FAIL.getCode(),"请重新登陆后操作");
        }
        log.info("删除评分类型结束！");
        return sysScoreTypeService.delete(id);
    }

    /**
     * 根据id查询评分类型信息
     * @param id
     * @param session
     * @return
     */
    @RequestMapping("/getScoreTypeById")
    public SysScoreTypeDTO getScoreTypeById(String id, HttpSession session) {
        //登陆名
        String loginName = (String) session.getAttribute("loginName");
        log.info("删除评分类型开始！id："+id);
        if (StringUtils.isBlank(loginName)){
            log.info("请重新登陆后操作！");
            return null;
        }
        log.info("删除评分类型结束！");
        return sysScoreTypeService.getScoreTypeById(id);
    }

    /**
     * 获取评分列表
     * @param session 缓存信息
     * @return 企业信用得分
     */
    @RequestMapping(value = "/getScoreTypeList")
    public  List<SysScoreTypeDTO> getList(String scoreType, HttpSession session) {
        log.info("获取评分列表开始，评分类型：");
        List<SysScoreTypeDTO> list = sysScoreTypeService.getScoreTypeListByScoreType(scoreType);
        log.info("获取评分列表结束！list.size:"+list.size());
        return list;
    }

}
